<template>
  <!--只支持单个文件上传-->
  <div class="div">
    <!-- :on-preview="handlePreview"-->
    <div>
      <el-upload
        :data="fileUploadInfoDto"
        :action="actionUrl"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
        :disabled="isDisabled"
        class="avatar-uploader"
        name="dmsFile" >
        <img v-if="image" :src="image" class="avatar" @error="show">
        <i v-else class="el-icon-plus avatar-uploader-icon"/>
        <el-dialog :visible.sync="dialogVisible" title="图片" append-to-body>
          <img :src="image" width="100%" alt="">
        </el-dialog>
      </el-upload>
    </div>
    <div class="divB">
      <el-button v-if="image" @click="handlePreview">预览</el-button>
    </div>
  </div>
</template>

<script>
import { queryFile } from '@/api/basedata/file.js'
import SERVER from '@/api/server'
export default {
  // eslint-disable-next-line vue/require-prop-types
  props: ['imageStyle', 'fileType', 'fileDesc', 'maxSize', 'billId', 'isDisabled'],
  data() {
    return {
      image: '',
      fileUploadInfoDto: {
        billType: '',
        billId: ''
      },
      actionUrl: `${SERVER.DMSCLOUD_BASEDATA}/file/save/1`,
      over: false,
      imageHave: false,
      dialogVisible: false,
      dialogImageUrl: ''
    }
  },
  computed: {
  },
  watch: {
    billId(newValue, oldvalue) {
      if (newValue !== oldvalue) {
        this.fileUploadInfoDto.billId = this.newValue
        if (this.billId === '') {
          this.image = ''
        } else {
          this.imageFile()
        }
      }
    }
  },

  mounted() {
    if (this.imageStyle != null && this.imageStyle !== '') {
      this.fileUploadInfoDto.billType = this.imageStyle
    }
    if (this.billId != null && this.billId !== '') {
      this.fileUploadInfoDto.billId = this.billId
      this.imageFile()
    }
  },

  methods: {
    // 展示图片
    handleAvatarSuccess(res, file) {
      this.image = URL.createObjectURL(file.raw)
      this.imageHave = true
      // 用于向父组件传递已上传图片的id
      this.$emit('sendValueToParent', this.imageStyle, res.fileUploadId)
    },
    handlePreview(file) {
      this.dialogVisible = true
    },
    // 覆盖已选图片
    beforeAvatarUpload(file) {
      if (this.fileType != null && this.fileType !== '' && this.fileType !== undefined && this.fileDesc != null && this.fileDesc !== '' && this.fileDesc !== undefined && this.fileType.indexOf(file.name.substring(file.name.lastIndexOf('.'), file.name.length)) < 0) {
        this.$message({
          type: 'error',
          message: this.fileDesc
        })
        return false
      }
      if (this.maxSize != null && this.maxSize !== '' && this.maxSize !== undefined && file.size / 1024 / 1024 > parseInt(this.maxSize)) {
        this.$message({
          type: 'error',
          message: '上传文件大小需小于' + this.maxSize + 'MB!'
        })
        return false
      }
    },
    async imageFile() {
      queryFile(this.fileUploadInfoDto.billType, this.billId).then(response => {
        this.fileUploadIdStr = ''
        // let fileList = response.initialPreviewConfig;
        const urls = response.initialPreview
        this.image = `${SERVER.DMSCLOUD_BASEDATA}` + urls[0] + '?t=' + Math.random()
      })/* .catch(err => {
                this.$message({
                  type: 'warning',
                  message: '附件下载失败!'
                })
              })*/
    },
    show() {
      this.image = false
    }
  }
}
</script>
<style scoped>
  .div{
    width: 178px;
    height: 218px;
  }
  .divB{
    margin-top: 6px;
    text-align: center;
  }
  .avatar-uploader {
    width: 178px;
    height: 178px;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader :hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
